package com.cuitrip.business.tripservice;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.cuitrip.business.tripservice.model.CategoryInfo;
import com.cuitrip.component.para.ParaListView;
import com.cuitrip.component.para.model.Para;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.jumper.Jumper;
import com.lab.utils.ArrayUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 创建行程
 * Created  ak
 */
public class EditTripHighLightActivity extends ParaEditActivity{


    public static final int TAG_REQUEST_CODE = 0x01;

    @Bind(R.id.category_content)
    ParaListView mCategoryListView;

    @Bind(R.id.tag_layout)
    View tagLayout;
    private Animation shakeAnimation;
    private ArrayList<CategoryInfo> categoryList = new ArrayList<>();

    @Override
    protected void saveOtherAction(Intent intent) {
        intent.putExtra(EditTripTagsActivity.TAG_KEY, categoryList);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.page_edit_highlight;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shakeAnimation = AnimationUtils.loadAnimation(this, R.anim.shake);
        mCategoryListView.setCircleColor(R.color.qiaomai_7c706e);
        if (serviceInfo != null && !ArrayUtil.isNullOrEmpty(serviceInfo.getCateList())){
            categoryList.addAll(serviceInfo.getCateList());
        }
        initTags();
    }

    private void initTags(){
        if ( !ArrayUtil.isNullOrEmpty(categoryList)){
            mCategoryListView.setVisibility(View.VISIBLE);
            List<String> paraList = new ArrayList<>();
            for (CategoryInfo categoryInfo : categoryList){
                paraList.add(categoryInfo.getName());
            }
            bindParaItem(mCategoryListView, paraList);
        }else{
            mCategoryListView.setVisibility(View.GONE);
        }
    }

    private void bindParaItem(ParaListView paraListView, List<String> paraOutput) {
        if (!ArrayUtil.isNullOrEmpty(paraOutput)) {
            List<Para> paraList = new ArrayList<>();
            for (CharSequence charSequence : paraOutput) {
                if (TextUtils.isEmpty(charSequence)) {
                    continue;
                }
                paraList.add(new Para(charSequence));
            }
            paraListView.setParas(paraList);
        }
    }

    @Override
    protected boolean saveAction() {
        if (ArrayUtil.isNullOrEmpty(categoryList)){
            tagLayout.startAnimation(shakeAnimation);
            return false;
        }else{
            return super.saveAction();
        }
    }

    @OnClick(R.id.tag_layout)
    public void navigate2EditTag(){
        if (serviceInfo.getEditMode().ordinal() == TripEditActivity.ContentMode.NEW_MODE.ordinal()){
            TrackUtil.trackScreenName("/insider/trips/create/set/highlights/categories");
        }else{
            TrackUtil.trackScreenName("/insider/trips/edit/set/highlights/categories");
        }
        Intent intent = new Intent(this, EditTripTagsActivity.class);
        intent.putParcelableArrayListExtra(EditTripTagsActivity.TAG_KEY, categoryList);
        Jumper.launchActivityForResult(this, intent, TAG_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAG_REQUEST_CODE){
            if (resultCode == RESULT_OK){
                if(data.hasExtra(EditTripTagsActivity.TAG_KEY)){
                    categoryList = data.getParcelableArrayListExtra(EditTripTagsActivity.TAG_KEY);
                }else{
                    categoryList.clear();
                }
                initTags();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
